/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Actions;

import Users.Customer;
import com.opensymphony.xwork2.ActionSupport;
import db.DBConnector;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Adjell Ian
 */
public class ViewDetails extends ActionSupport {

    private Customer customer = new Customer();
    private DBConnector db;
    private String message;

    public ViewDetails() {
    }

    @Override
    public String execute() throws Exception {
        try {
            PreparedStatement pstmt = null;
            ResultSet rs = null;
            setDb(new DBConnector());
            Connection conn = null;
            String user;
            int customerid = 1;
            int creditID = 1;

            conn = getDb().getConnection();

            pstmt = conn.prepareStatement("select * from customers where username =  ? ");

            pstmt.setString(1, getCustomer().getUsername());

            rs = pstmt.executeQuery();

            while (rs.next()) {
                customerid = rs.getInt("customerID");
                getCustomer().setUsername(rs.getString("username"));
                getCustomer().setPassword(rs.getString("password"));
                getCustomer().setFname(rs.getString("customerfname"));
                getCustomer().setLname(rs.getString("customerlname"));
                getCustomer().setMiddle(rs.getString("customerm"));
                getCustomer().setEmail(rs.getString("email"));
            }

            pstmt.clearParameters();

            pstmt = conn.prepareStatement("select address.* from address where aID in (select deliveryaddress.addID from deliveryaddress inner join customers on deliveryaddress.custID = customers.customerID where customers.customerID = ?)");

            pstmt.setInt(1, customerid);
            
            rs = pstmt.executeQuery();
            
            while(rs.next())
            {
                getCustomer().getDelivery().setNo(rs.getInt("number"));
                getCustomer().getDelivery().setStreet(rs.getString("street"));
                getCustomer().getDelivery().setSubdivision(rs.getString("subdivision"));
                getCustomer().getDelivery().setCity(rs.getString("city"));
                getCustomer().getDelivery().setCountry(rs.getString("country"));
                getCustomer().getDelivery().setPostal(rs.getInt("postal"));
            }
            
            pstmt.clearParameters();
            
            pstmt = conn.prepareStatement("select creditcard.* from creditcard where creditID in (select creditowner.creditID from creditowner inner join customers on creditowner.customerID = customers.customerID where customers.customerID = ?)");
            
            pstmt.setInt(1, customerid);
            
            rs = pstmt.executeQuery();
            
            while(rs.next())
            {
                creditID = rs.getInt("creditID");
                getCustomer().getCredit().setNum(rs.getInt("creditNo"));
                getCustomer().getCredit().setName(rs.getString("creditName"));
                getCustomer().getCredit().setIssuer(rs.getInt("issuer"));
                getCustomer().getCredit().setExpdate(rs.getDate("expdate"));
            }
            
            pstmt.clearParameters();
            
            pstmt = conn.prepareStatement("select address.* from address where aID in (select billingaddress.addressID from billingaddress inner join creditcard on billingaddress.creditID = creditcard.creditID where creditcard.creditID = ?)");
            
            pstmt.setInt(1, creditID);
            
            rs = pstmt.executeQuery();
            
            while(rs.next())
            {
                getCustomer().getCredit().getBilling().setNo(rs.getInt("number"));
                getCustomer().getCredit().getBilling().setStreet(rs.getString("street"));
                getCustomer().getCredit().getBilling().setSubdivision(rs.getString("subdivision"));
                getCustomer().getCredit().getBilling().setCity(rs.getString("city"));
                getCustomer().getCredit().getBilling().setCountry(rs.getString("country"));
                getCustomer().getCredit().getBilling().setPostal(rs.getInt("postal"));
            }
            conn.close();

            return SUCCESS;
        } catch (Exception e) {
            Logger.getLogger(ViewDetails.class.getName()).log(Level.SEVERE, null, e);
            setMessage(e.getMessage());
            return ERROR;
        }
    }

    /**
     * @return the customer
     */
    public Customer getCustomer() {
        return customer;
    }

    /**
     * @param customer the customer to set
     */
    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    /**
     * @return the db
     */
    public DBConnector getDb() {
        return db;
    }

    /**
     * @param db the db to set
     */
    public void setDb(DBConnector db) {
        this.db = db;
    }

    /**
     * @return the message
     */
    public String getMessage() {
        return message;
    }

    /**
     * @param message the message to set
     */
    public void setMessage(String message) {
        this.message = message;
    }
}
